package com.crm.service.system.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.crm.domain.dto.SysMenuDto;
import com.crm.domain.entity.SysMenu;
import com.crm.domain.query.MenuQuery;
import com.crm.mapper.MenuMapper;
import com.crm.service.system.MenuService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class MenuServiceImpl extends ServiceImpl<MenuMapper, SysMenu> implements MenuService {
    private final MenuMapper menuMapper;

    /**
     * 查询所有菜单列表
     * @param query
     * @return
     */
    @Override
    public List<SysMenu> queryAll(MenuQuery query) {

        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.<SysMenu>lambdaQuery()
                .like(query.getMenuName() != null, SysMenu::getMenuName, query.getMenuName())
                .eq(query.getStatus() != null, SysMenu::getStatus, query.getStatus());

        return menuMapper.selectList(wrapper);
    }

    /**
     * 获取菜单下拉树结构
     * @return
     */
    /*@Override
    public List<TreeItemVo> treeSelect() {
        //1 查询所有菜单
        LambdaQueryWrapper<SysMenu> wrapper = Wrappers.lambdaQuery();
        List<SysMenu> menus = menuMapper.selectList(wrapper);
        //2 构建菜单树
         //2.1 获取一级菜单
         //2.2 获取二级菜单
         //2.3 获取三级菜单

        //3 返回菜单树

        return null;
    }*/

    @Override
    public void updateMenu(SysMenuDto menuDto) {
        SysMenu sysMenu = BeanUtil.copyProperties(menuDto, SysMenu.class);
        LambdaUpdateWrapper<SysMenu> wrapper = Wrappers.<SysMenu>lambdaUpdate()
                .eq(SysMenu::getMenuId,menuDto.getMenuId());

        menuMapper.update(sysMenu,wrapper);
    }
}
